package com.youi.job;

import com.google.common.collect.Maps;
import com.youi.common.Common;
import com.youi.common.DelStatus;
import com.youi.entity.Goods;
import com.youi.service.GoodsService;
import com.youi.util.DateUtil;
import com.youi.util.LogUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @description: 商品相关异步定时任务
 * @Author: winty
 * @Date: 2020/5/27 11:11
 */
@Component
public class GoodsJob {

    @Autowired
    private GoodsService goodsService;

    /**
     * 把未出售的商品转为出售中
     * 每分钟执行一次
     */
    @Async
    @Scheduled(cron = "0 */1 * * * ?")
    public void changeGoodsStatusStarted() {
        LogUtil.Info("****商品定时任务****变为出售中开始****");

        Map<String, Object> param = Maps.newHashMap();
        param.put(Common.DELSTATUS, DelStatus.NODEL);
        param.put(Common.STATUS, 4);

        Map<String, Object> ltParam = new HashMap<String, Object>();
        ltParam.put("gmtStart", DateUtil.simpdfyMdHms.format(new Date()));

        List<Goods> goodsList = goodsService.getByParamsAll(param, null, null, null, ltParam);
        if (CollectionUtils.isEmpty(goodsList)) {
            return;
        }
        List<Long> goodsIdList = goodsList.stream().map(Goods::getId).collect(Collectors.toList());

        try {
            goodsService.batchUpdateGoodsStatus(goodsIdList, 1);
        } catch (Exception e) {
            LogUtil.Error("****商品定时任务****变为出售中失败:" + e.getMessage());
        }
    }

    /**
     * 把出售中的商品转为已结束
     * 每分钟执行一次
     */
    @Async
    @Scheduled(cron = "0 */1 * * * ?")
    public void changeGoodsStatusFinished() {
        LogUtil.Info("****商品定时任务****变为已结束开始****");

        Map<String, Object> param = Maps.newHashMap();
        param.put(Common.DELSTATUS, DelStatus.NODEL);
        param.put(Common.STATUS, 1);

        Map<String, Object> ltParam = new HashMap<String, Object>();
        ltParam.put("gmtEnd", DateUtil.simpdfyMdHms.format(new Date()));

        List<Goods> goodsList = goodsService.getByParamsAll(param, null, null, null, ltParam);
        if (CollectionUtils.isEmpty(goodsList)) {
            return;
        }
        List<Long> goodsIdList = goodsList.stream().map(Goods::getId).collect(Collectors.toList());

        try {
            goodsService.batchUpdateGoodsStatus(goodsIdList, 5);
        } catch (Exception e) {
            LogUtil.Error("****商品定时任务****变为已结束失败：" + e.getMessage());
        }
    }

    /**
     * 把库存为0的商品变为已售罄
     * 每分钟执行一次
     */
    @Async
    @Scheduled(cron = "0 */1 * * * ?")
    public void changeGoodsStatusSellOut() {
        LogUtil.Info("****商品定时任务****变为已售罄开始****");

        List<Goods> goodsList = goodsService.listNoStockGoods(new HashMap<String, Object>(1) {{
            put(Common.STATUS, 1);
        }});
        if (CollectionUtils.isEmpty(goodsList)) {
            return;
        }
        List<Long> goodsIdList = goodsList.stream().map(Goods::getId).collect(Collectors.toList());

        try {
            goodsService.batchUpdateGoodsStatus(goodsIdList, 2);
        } catch (Exception e) {
            LogUtil.Error("****商品定时任务****变为已售罄失败：" + e.getMessage());
        }
    }

}